import { Controller, Post, Body, Inject, Req } from '@nestjs/common';
import { LikeService } from './like.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { BlogService } from '../blog/blog.service';
import { CommentService } from 'src/comment/comment.service';
import { DailyService } from 'src/daily/daily.service';
import { Request } from 'express';
@Controller('like')
export class LikeController {
  constructor(private readonly likeService: LikeService) {}

  @Inject(BlogService)
  private blogService: BlogService;

  @Inject(CommentService)
  private commentService: CommentService;

  @Inject(DailyService)
  private dailyService: DailyService;
  // 点赞
  // @RequireLogin()
  @Post('add')
  async like(
    @Body('createId') createId: number,
    // @UserInfo('userId') userId: number,
    @Body('userId') userId: number,
    @Body('type') type: number,
    @Body('authorId') authorId: number,
    @Req() req: Request
  ) {
    return await this.likeService.create({
      createId: createId,
      userId: userId,
      type: type,
      authorId: authorId,
      ip: req.ip
    });
    // const types = ['blogService', 'commentService', 'dailyService']
    // await this[types[type]].like(createId)
    // return res
  }

  // 取消点赞
  @RequireLogin()
  @Post('del')
  async unlike(
    @Body('createId') createId: number,
    @Body('id') id: number,
    @UserInfo('userId') userId: number,
    @Body('type') type: number,
    @Req() req: Request
  ) {
    await this.likeService.remove({ id, createId, userId, type });
  }

  @RequireLogin()
  @Post('findByCreateIdAndType')
  async findByCreateIdAndType(
    @Body('createId') createId: number,
    @Body('type') type: number,
    @Body('userId') userId: number,
  ) {
    return await this.likeService.findByCreateIdAndType({
      createId,
      type,
      userId,
    });
  }

  @RequireLogin()
  @Post('queryLikeTotal')
  async queryLikeTotal(@UserInfo('userId') userId: number) {
    return await this.likeService.queryLikeTotal(userId);
  }
}
